home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Mac-Source 1994 July
/
Mac-Source_July_1994.iso
/
Other Langs
/
Tickle-4.0 (tcl)
/
src
/
mactoasd.c
< prev
next >
Wrap
Text File
|
1993-11-18
|
15KB
|
652 lines
/*
** This source code was written by Tim Endres
** Email: time@ice.com.
** USMail: 8840 Main Street, Whitmore Lake, MI 48189
**
** Some portions of this application utilize sources
** that are copyrighted by ICE Engineering, Inc., and
** ICE Engineering retains all rights to those sources.
**
** Neither ICE Engineering, Inc., nor Tim Endres,
** warrants this source code for any reason, and neither
** party assumes any responsbility for the use of these
** sources, libraries, or applications. The user of these
** sources and binaries assumes all responsbilities for
** any resulting consequences.
*/
#pragma segment ASD
#include <types.h>
#include <errors.h>
#include <toolutils.h>
#include <standardfile.h>
#include <fcntl.h>
#include <stdio.h>
#include "asd.h"
#include "mb.h"
#include "defines.h"
#ifndef TRUE
# define TRUE (Boolean)1
#endif
#ifndef FALSE
# define FALSE (Boolean)0
#endif
#undef BLK_SIZE
#define BLK_SIZE 1024
#define iobuffer kBuffer
extern char kBuffer[];
/* static char iobuffer[BLK_SIZE]; */
static long dlength = 0;
static long rlength = 0;
static long cdate = 0;
static long mdate = 0;
/*
** WARNING ! WARNING ! WARNING !
** Both of the parameters to this macro
** are used more than once! Also, I did
** NOT parenthsize any parameters.
*/
#define CHAR4_TO_LONG(char4, long) \
{ \
long = 0; \
long += char4 [0]; long <<= 8; \
long += char4 [1]; long <<= 8; \
long += char4 [2]; long <<= 8; \
long += char4 [3]; \
}
#ifdef TCLAPPL
macintosh_to_asingle(crnl_flag)
int crnl_flag;
{
SFReply macreply,
asreply;
Point mypoint;
SFTypeList mytypes;
int result;
char orig[64];
extern int errno;
mypoint.h = mypoint.v = 75;
MyGetFile(mypoint, "\pMacintosh File:", NULL, -1, mytypes, NULL, ¯eply);
if (!macreply.good)
return CANCEL;
sprintf(orig, "%.*s.AS", macreply.fName[0], ¯eply.fName[1]);
c2pstr(orig);
MyPutFile(mypoint, "\pApple Single File:", orig, NULL, &asreply);
if (!asreply.good)
return CANCEL;
p2cstr(asreply.fName);
p2cstr(macreply.fName);
result = do_mac_to_asingle( macreply.fName, macreply.vRefNum,
asreply.fName, asreply.vRefNum, TRUE, crnl_flag );
return (result == noErr ? SUCCESS : FAILURE);
}
#endif /* TCLAPPL */
do_mac_to_asingle(macname, macVRef, asname, asVRef, interactive, crnl_flag)
char *macname; /* C */
int macVRef;
char *asname; /* C */
int asVRef;
int interactive;
int crnl_flag;
{
char *ptr;
int i, myerr;
int blocks;
int residue;
int offset;
FILE *asfile;
ParamBlockRec pb, iopb;
extern int errno;
pb.fileParam.ioCompletion = 0;
pb.fileParam.ioVRefNum = macVRef;
pb.fileParam.ioNamePtr = macname;
pb.fileParam.ioFVersNum = 0;
pb.fileParam.ioFDirIndex = 0;
c2pstr(macname);
myerr = PBGetFInfo(&pb, FALSE);
p2cstr(macname);
if (myerr != noErr)
{
Feedback("Error #%d getting Macintosh file info.", errno);
return myerr;
}
SetVol(NULL, asVRef);
asfile = fopen(asname, "w");
if (asfile == NULL)
{
Feedback("Error #%d opening apple single file '%s'.", errno, asname);
return (errno == 0 ? fnfErr : errno);
}
UBegYield();
RotateCursor(32);
DoYield();
dlength = pb.fileParam.ioFlLgLen;
rlength = pb.fileParam.ioFlRLgLen;
cdate = pb.fileParam.ioFlCrDat;
mdate = pb.fileParam.ioFlMdDat;
cdate = MAC_TO_UNIX_TIME(cdate);
mdate = MAC_TO_UNIX_TIME(mdate);
if (interactive)
{
Feedback("Macintosh file '%.*s'", macname);
Feedback("File Type '%4.4s'", &pb.fileParam.ioFlFndrInfo.fdType);
Feedback("File Creator '%4.4s'", &pb.fileParam.ioFlFndrInfo.fdCreator);
Feedback("Data Fork %d bytes.", dlength);
Feedback("Rsrc Fork %d bytes.", rlength);
Feedback("Creation Date [%d] %s", cdate, ctime(&cdate));
Feedback("Modification Date [%d] %s", mdate, ctime(&mdate));
}
RotateCursor(32);
DoYield();
memcpy(&asd_finder, &pb.fileParam.ioFlFndrInfo, sizeof(asd_fndr_fork));
asd_file_head.asd_head.magic = MAGIC_APPLE_SINGLE;
asd_file_head.asd_head.version = VERSION_APPLE;
sprintf(asd_file_head.asd_head.filesys, "%-16.16s", "Macintosh");
asd_file_head.asd_head.entries = 3;
i = 0;
asd_file_head.asd_entries[i].id = ASDID_FNDR_FORK;
asd_file_head.asd_entries[i].offset = sizeof(asd_file_hdr);
asd_file_head.asd_entries[i].length = sizeof(asd_fndr_fork);
i++;
asd_file_head.asd_entries[i].id = ASDID_RSRC_FORK;
asd_file_head.asd_entries[i].offset =
asd_file_head.asd_entries[i-1].offset +
asd_file_head.asd_entries[i-1].length;
asd_file_head.asd_entries[i].length = rlength;
i++;
asd_file_head.asd_entries[i].id = ASDID_DATA_FORK;
asd_file_head.asd_entries[i].offset =
asd_file_head.asd_entries[i-1].offset +
asd_file_head.asd_entries[i-1].length;
asd_file_head.asd_entries[i].length = dlength;
RotateCursor(32);
DoYield();
fwrite(&asd_file_head, 1, sizeof(asd_file_hdr), asfile);
fwrite(&asd_finder, 1, sizeof(asd_fndr_fork), asfile);
offset = MAC_BINARY_HDR_SIZE + (((dlength + BLK_SIZE - 1) >> 7) << 7);
if (rlength > 0)
{
pb.ioParam.ioCompletion = 0;
pb.ioParam.ioVRefNum = macVRef;
pb.ioParam.ioNamePtr = macname;
pb.ioParam.ioVersNum = 0;
pb.ioParam.ioPermssn = fsRdPerm;
pb.ioParam.ioMisc = 0;
c2pstr(macname);
myerr = PBOpenRF(&pb, FALSE);
p2cstr(macname);
if (myerr != noErr)
{
}
else {
blocks = rlength / BLK_SIZE;
residue = rlength % BLK_SIZE;
for ( ; blocks; blocks-- )
{
RotateCursor(32);
DoYield();
if (pause_op)
while (pause_op)
pausing();
iopb.ioParam.ioCompletion = 0;
iopb.ioParam.ioRefNum = pb.fileParam.ioFRefNum;
iopb.ioParam.ioBuffer = iobuffer;
iopb.ioParam.ioReqCount = BLK_SIZE;
iopb.ioParam.ioPosMode = fsAtMark;
iopb.ioParam.ioPosOffset = 0;
myerr = PBRead(&iopb, FALSE);
if (myerr == noErr)
fwrite(iobuffer, 1, BLK_SIZE, asfile);
}
if (residue)
{
RotateCursor(32);
DoYield();
if (pause_op)
while (pause_op)
pausing();
iopb.ioParam.ioCompletion = 0;
iopb.ioParam.ioRefNum = pb.fileParam.ioFRefNum;
iopb.ioParam.ioBuffer = iobuffer;
iopb.ioParam.ioReqCount = residue;
iopb.ioParam.ioPosMode = fsAtMark;
iopb.ioParam.ioPosOffset = 0;
myerr = PBRead(&iopb, FALSE);
if (myerr == noErr)
fwrite(iobuffer, 1, residue, asfile);
}
PBClose(&pb, FALSE);
}
}
if (dlength > 0)
{
pb.ioParam.ioCompletion = 0;
pb.ioParam.ioVRefNum = macVRef;
pb.ioParam.ioNamePtr = macname;
pb.ioParam.ioVersNum = 0;
pb.ioParam.ioPermssn = fsRdPerm;
pb.ioParam.ioMisc = 0;
c2pstr(macname);
myerr = PBOpen(&pb, FALSE);
p2cstr(macname);
if (myerr != noErr)
{
}
else
{
blocks = dlength / BLK_SIZE;
residue = dlength % BLK_SIZE;
for ( ; blocks; blocks-- )
{
RotateCursor(32);
DoYield();
if (pause_op)
while (pause_op)
pausing();
iopb.ioParam.ioCompletion = 0;
iopb.ioParam.ioRefNum = pb.fileParam.ioFRefNum;
iopb.ioParam.ioBuffer = iobuffer;
iopb.ioParam.ioReqCount = BLK_SIZE;
iopb.ioParam.ioPosMode = fsAtMark;
iopb.ioParam.ioPosOffset = 0;
myerr = PBRead(&iopb, FALSE);
if (myerr == noErr)
{
if (crnl_flag)
for (i=iopb.ioParam.ioActCount, ptr=iobuffer; i; i--, ptr++)
if (*ptr == 0x0D) *ptr = 0x0A;
fwrite(iobuffer, 1, BLK_SIZE, asfile);
}
}
if (residue)
{
RotateCursor(32);
DoYield();
if (pause_op)
while (pause_op)
pausing();
iopb.ioParam.ioCompletion = 0;
iopb.ioParam.ioRefNum = pb.fileParam.ioFRefNum;
iopb.ioParam.ioBuffer = iobuffer;
iopb.ioParam.ioReqCount = residue;
iopb.ioParam.ioPosMode = fsAtMark;
iopb.ioParam.ioPosOffset = 0;
myerr = PBRead(&iopb, FALSE);
if (myerr == noErr)
{
if (crnl_flag)
for (i=iopb.ioParam.ioActCount, ptr=iobuffer; i; i--, ptr++)
if (*ptr == 0x0D) *ptr = 0x0A;
fwrite(iobuffer, 1, residue, asfile);
}
}
PBClose(&pb, FALSE);
}
}
WatchCursorOn();
fclose(asfile);
set_file_type(asname, asVRef, APPL_TYPE, (OSType)'AS/D');
UInitCursor();
UEndYield();
return noErr;
}
#ifdef TCLAPPL
macintosh_to_adouble(crnl_flag)
int crnl_flag;
{
SFReply macreply,
rreply,
dreply;
Point mypoint;
SFTypeList mytypes;
char orig[64];
int result;
extern int errno;
mypoint.h = mypoint.v = 75;
MyGetFile(mypoint, "\pMacintosh File:", NULL, -1, mytypes, NULL, ¯eply);
if (!macreply.good)
return CANCEL;
sprintf(orig, "%.*s.AD", macreply.fName[0], ¯eply.fName[1]);
c2pstr(orig);
MyPutFile(mypoint, "\pApple Double Data File:", orig, NULL, &dreply);
if (!dreply.good)
return CANCEL;
sprintf(orig, "%%%.*s",
( (dreply.fName[0] > 26) ? 26 : dreply.fName[0] ), &dreply.fName[1]);
c2pstr(orig);
MyPutFile(mypoint, "\pApple Double Rsrc File:", orig, NULL, &rreply);
if (!rreply.good)
return CANCEL;
p2cstr(dreply.fName);
p2cstr(rreply.fName);
p2cstr(macreply.fName);
result = do_mac_to_adouble( macreply.fName, macreply.vRefNum,
rreply.fName, rreply.vRefNum,
dreply.fName, dreply.vRefNum, TRUE, crnl_flag );
return (result == noErr ? SUCCESS : FAILURE);
}
#endif /* TCLAPPL */
do_mac_to_adouble( macname, macVRef, adrname, adrVRef,
addname, addVRef, interactive, crnl_flag )
char *macname; /* C */
int macVRef;
char *adrname; /* C */
int adrVRef;
char *addname; /* C */
int addVRef;
int interactive;
int crnl_flag;
{
ParamBlockRec pb, iopb;
char *ptr;
int i, myerr;
int blocks;
int residue;
FILE *rfile, *dfile;
extern int errno;
pb.fileParam.ioCompletion = 0;
pb.fileParam.ioVRefNum = macVRef;
pb.fileParam.ioNamePtr = macname;
pb.fileParam.ioFVersNum = 0;
pb.fileParam.ioFDirIndex = 0;
c2pstr(macname);
myerr = PBGetFInfo(&pb, FALSE);
p2cstr(macname);
if (myerr != noErr)
{
Feedback("Error #%d getting Macintosh file info.", errno);
return FAILURE;
}
SetVol(NULL, adrVRef);
rfile = fopen(adrname, "w");
if (rfile == NULL)
{
Feedback("Error #%d opening apple double rsrc file '%s'.", errno, adrname);
return FAILURE;
}
SetVol(NULL, addVRef);
dfile = fopen(addname, "w");
if (dfile == NULL)
{
Feedback("Error #%d opening apple double data file '%s'.", errno, addname);
fclose(rfile);
return FAILURE;
}
UBegYield();
RotateCursor(32);
DoYield();
dlength = pb.fileParam.ioFlLgLen;
rlength = pb.fileParam.ioFlRLgLen;
cdate = pb.fileParam.ioFlCrDat;
mdate = pb.fileParam.ioFlMdDat;
cdate = MAC_TO_UNIX_TIME(cdate);
mdate = MAC_TO_UNIX_TIME(mdate);
if (interactive)
{
Feedback("Macintosh file '%s'", macname);
Feedback("File Type '%4.4s'", &pb.fileParam.ioFlFndrInfo.fdType);
Feedback("File Creator '%4.4s'", &pb.fileParam.ioFlFndrInfo.fdCreator);
Feedback("Data Fork %d bytes.", dlength);
Feedback("Rsrc Fork %d bytes.", rlength);
Feedback("Creation Date [%d] %s", cdate, ctime(&cdate));
Feedback("Modification Date [%d] %s", mdate, ctime(&mdate));
}
RotateCursor(32);
DoYield();
memcpy(&asd_finder, &pb.fileParam.ioFlFndrInfo, sizeof(asd_fndr_fork));
asd_file_head.asd_head.magic = MAGIC_APPLE_DOUBLE;
asd_file_head.asd_head.version = VERSION_APPLE;
sprintf(asd_file_head.asd_head.filesys, "%-16.16s", "Macintosh");
i = 0;
asd_file_head.asd_entries[i].id = ASDID_FNDR_FORK;
asd_file_head.asd_entries[i].offset = sizeof(asd_file_hdr);
asd_file_head.asd_entries[i].length = sizeof(asd_fndr_fork);
i++;
asd_file_head.asd_entries[i].id = ASDID_RSRC_FORK;
asd_file_head.asd_entries[i].offset =
asd_file_head.asd_entries[i-1].offset +
asd_file_head.asd_entries[i-1].length;
asd_file_head.asd_entries[i].length = rlength;
i++;
RotateCursor(32);
DoYield();
asd_file_head.asd_head.entries = i;
fwrite(&asd_file_head, 1, sizeof(asd_file_hdr), rfile);
fwrite(&asd_finder, 1, sizeof(asd_fndr_fork), rfile);
Feedback("Mac-RSRC Length %ld", rlength);
if (rlength > 0)
{
pb.ioParam.ioCompletion = 0;
pb.ioParam.ioVRefNum = macVRef;
pb.ioParam.ioNamePtr = macname;
pb.ioParam.ioVersNum = 0;
pb.ioParam.ioPermssn = fsRdPerm;
pb.ioParam.ioMisc = 0;
c2pstr(macname);
myerr = PBOpenRF(&pb, FALSE);
p2cstr(macname);
if (myerr != noErr)
{
}
else
{
blocks = rlength / BLK_SIZE;
residue = rlength % BLK_SIZE;
for ( ; blocks; blocks-- )
{
RotateCursor(32);
DoYield();
if (pause_op)
while (pause_op)
pausing();
iopb.ioParam.ioCompletion = 0;
iopb.ioParam.ioRefNum = pb.fileParam.ioFRefNum;
iopb.ioParam.ioBuffer = iobuffer;
iopb.ioParam.ioReqCount = BLK_SIZE;
iopb.ioParam.ioPosMode = fsAtMark;
iopb.ioParam.ioPosOffset = 0;
myerr = PBRead(&iopb, FALSE);
if (myerr == noErr)
fwrite(iobuffer, 1, BLK_SIZE, rfile);
}
if (residue)
{
RotateCursor(32);
DoYield();
if (pause_op)
while (pause_op)
pausing();
iopb.ioParam.ioCompletion = 0;
iopb.ioParam.ioRefNum = pb.fileParam.ioFRefNum;
iopb.ioParam.ioBuffer = iobuffer;
iopb.ioParam.ioReqCount = residue;
iopb.ioParam.ioPosMode = fsAtMark;
iopb.ioParam.ioPosOffset = 0;
myerr = PBRead(&iopb, FALSE);
if (myerr == noErr)
fwrite(iobuffer, 1, residue, rfile);
}
PBClose(&pb, FALSE);
}
}
Feedback("Mac-DATA Length %ld", dlength);
if (dlength > 0)
{
pb.ioParam.ioCompletion = 0;
pb.ioParam.ioVRefNum = macVRef;
pb.ioParam.ioNamePtr = macname;
pb.ioParam.ioVersNum = 0;
pb.ioParam.ioPermssn = fsRdPerm;
pb.ioParam.ioMisc = 0;
c2pstr(macname);
myerr = PBOpen(&pb, FALSE);
p2cstr(macname);
if (myerr != noErr)
{
}
else
{
blocks = dlength / BLK_SIZE;
residue = dlength % BLK_SIZE;
for ( ; blocks; blocks-- )
{
RotateCursor(32);
DoYield();
if (pause_op)
while (pause_op)
pausing();
iopb.ioParam.ioCompletion = 0;
iopb.ioParam.ioRefNum = pb.fileParam.ioFRefNum;
iopb.ioParam.ioBuffer = iobuffer;
iopb.ioParam.ioReqCount = BLK_SIZE;
iopb.ioParam.ioPosMode = fsAtMark;
iopb.ioParam.ioPosOffset = 0;
myerr = PBRead(&iopb, FALSE);
if (myerr == noErr) {
if (crnl_flag)
for (i=iopb.ioParam.ioActCount, ptr=iobuffer; i; i--, ptr++)
if (*ptr == 0x0D) *ptr = 0x0A;
fwrite(iobuffer, 1, BLK_SIZE, dfile);
}
}
if (residue)
{
RotateCursor(32);
DoYield();
if (pause_op)
while (pause_op)
pausing();
iopb.ioParam.ioCompletion = 0;
iopb.ioParam.ioRefNum = pb.fileParam.ioFRefNum;
iopb.ioParam.ioBuffer = iobuffer;
iopb.ioParam.ioReqCount = residue;
iopb.ioParam.ioPosMode = fsAtMark;
iopb.ioParam.ioPosOffset = 0;
myerr = PBRead(&iopb, FALSE);
if (myerr == noErr)
{
if (crnl_flag)
for (i=iopb.ioParam.ioActCount, ptr=iobuffer; i; i--, ptr++)
if (*ptr == 0x0D) *ptr = 0x0A;
fwrite(iobuffer, 1, residue, dfile);
}
}
PBClose(&pb, FALSE);
}
}
WatchCursorOn();
fclose(rfile);
fclose(dfile);
set_file_type(adrname, adrVRef, APPL_TYPE, (OSType)'AS/D');
set_file_type(addname, addVRef, APPL_TYPE, (OSType)'TEXT');
UInitCursor();
UEndYield();
return SUCCESS;
}